package com.andengine.editor.sceneeditor;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.Timer;

/**
 * @author sad
 */
public class ScenePlane extends JPanel {

    BufferedImage im;
    BufferedImage child;
    private Point p1 = new Point(200, 200);
    private Point p2 = new Point(100, 0);
    private Point p3 = new Point(0, 100);
    private double rotation = 0;
    private double rotation2 = 0;
    private double rotation3 = 0;

    public ScenePlane() throws IOException {
       // im = ImageIO.read(new File("D:/1.png"));
       // child = ImageIO.read(new File("D:/2.png"));
        Timer t = new Timer(50, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                rotation++;
                rotation2 -= 2;
                rotation3 += 2;
                repaint();
            }
        });
        t.setRepeats(true);
        t.start();
        addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {

                /*AffineTransform t = new AffineTransform();
                 t.translate(p1.x - im.getWidth() / 2, p1.y - im.getHeight() / 2);
                 t.rotate(Math.toRadians(rotation), im.getWidth() / 2, im.getHeight() / 2);
                 t.scale(2, 1);
                 Point np = new Point();
                 t.inverseTransform(e.getPoint(), np);
                 Graphics g = im.getGraphics();
                 g.setColor(Color.BLUE);
                 g.fillRect(np.x, np.y, 2, 2);
                 g.dispose();
                 ((JFrame) SwingUtilities.getWindowAncestor(ScenePlane.this)).setTitle("x=" + np.x + "; y=" + np.y);*/
            }
        });
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        /*try {
            
            Graphics2D g2 = (Graphics2D) g;
            AffineTransform oldTransform = g2.getTransform();
            //g2.getTransform().setToIdentity();
            g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
            AffineTransform t = new AffineTransform();
            int wh = im.getWidth() / 2;
            int hh = im.getHeight() / 2;
            t.translate(p1.x - wh, p1.y - hh);
            t.rotate(Math.toRadians(rotation), im.getWidth() / 2, im.getHeight() / 2);
            t.scale(2, 1);
            g2.transform(t);
            g2.drawImage(im, 0, 0, this);

            g2.setColor(Color.GREEN);
            g2.fillRect(-3, -3, 2, 2);
            g2.fillRect(wh * 2 + 1, -3, 2, 2);
            g2.fillRect(wh * 2 + 1, hh * 2 + 1, 2, 2);
            g2.fillRect(-3, hh * 2 + 1, 2, 2);

            AffineTransform tChild = new AffineTransform();
            wh = child.getWidth() / 2;
            hh = child.getHeight() / 2;
            tChild.translate(p2.x, p2.y);
            tChild.rotate(Math.toRadians(rotation2), wh, hh);
            g2.transform(tChild);
            g2.drawImage(child, 0, 0, this);

            tChild.invert();
            g2.transform(tChild);

            AffineTransform tChild2 = new AffineTransform();
            wh = child.getWidth() / 2;
            hh = child.getHeight() / 2;
            tChild2.translate(p3.x, p3.y);
            tChild2.rotate(Math.toRadians(rotation3), wh, hh);
            g2.transform(tChild2);
            g2.drawImage(child, 0, 0, this);

            g2.setTransform(oldTransform);
        } catch (NoninvertibleTransformException ex) {
            Logger.getLogger(ScenePlane.class.getName()).log(Level.SEVERE, null, ex);
        }*/
    }
}
